<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\FeedbackInterface;
use App\Repository\Contracts\FeedbackJavaInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 19:16
 *
 * @Description: 反馈
 */
class FeedbackController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var FeedbackInterface
     */
    protected $feedback;

    /**
     * @var FeedbackJavaInterface
     */
    protected $feedbackJava;

    protected $uid;

    protected $storeId;

    /**
     * FeedbackController constructor.
     * @param Request $request
     * @param FeedbackInterface $feedback
     * @param FeedbackJavaInterface $feedbackJava
     */
    public function __construct(Request $request, FeedbackInterface $feedback, FeedbackJavaInterface $feedbackJava)
    {
        $this->request = $request;

        $this->feedback = $feedback;

        $this->feedbackJava = $feedbackJava;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];

        $this->storeId = CommonHelper::getToken(Auth::user())['storeId'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $list = $this->feedback->lists($limit, $page);

        return $this->success($list);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['quote_id', 'content']);
            $data['store_id'] = $this->storeId;
            $id = $this->feedback->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function syncList()
    {
        try {
            $storeId = $this->storeId;
            $maxId = $this->feedback->findIdForFirst()?:0;
            $result = $this->feedbackJava->syncList($storeId, $maxId);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}